﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Reflection;
using System.Windows.Media;

namespace ThreeByte.ActiveDeck.Converters
{
    public class PropertyInfoToColorConverter : IValueConverter
    {
        private static readonly PropertyInfo[] ColorPropertyInfos;

        static PropertyInfoToColorConverter() {
            ColorPropertyInfos = Type.GetType(@"System.Windows.Media.Colors, PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35").GetProperties();
        }


        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            Color color = (Color)value;

            PropertyInfo pi = (from p in ColorPropertyInfos
                              where ((Color)(p.GetValue(null, null))).Equals(color)
                              select p).SingleOrDefault();
            
            return pi;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            PropertyInfo pi = value as PropertyInfo;

            return ColorConverter.ConvertFromString(pi.Name);
        }
    }
   
}
